<template>
  <div class="system-user-dialog-container">
    <el-dialog :close-on-click-modal="false" :title="dataForm.userId ? $t('common.editBtn') : $t('common.addBtn')"
      draggable v-model="visible">
      <el-form :model="dataForm" :rules="dataRules" label-width="100px" ref="dataFormRef" v-loading="loading">
        <el-row :gutter="20">
          <el-col :span="12" class="mb20">
            <el-form-item :label="$t('message.module')" prop="Module">
              <el-input v-model="dataForm.module" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item :label="$t('message.type')" prop="Type">
              <el-input clearable disabled v-model="dataForm.type"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!--<el-col :span="12" class="mb20">
            <el-form-item :label="$t('message.userNo')" prop="userNameId">
              <el-input v-model="dataForm.userNameId"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item :label="$t('message.userName')" prop="userName">
              <el-input clearable v-model="dataForm.userName"></el-input>
            </el-form-item>
          </el-col>-->

          <el-col :span="12" class="mb20">
            <el-form-item prop="userList">
              <template #label>选择用户
                <tip content="选择用户" />
              </template>
              <!-- 组织架构   -->
              <org-selector v-model:orgList="dataForm.userList" type="user" :multiple="true" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="close">{{ $t('common.cancelButtonText') }}</el-button>
          <el-button @click="onSubmit" type="primary" icon="check" :disabled="loading">{{
            $t('common.confirmButtonText')
          }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="spDialog" setup>
import { useMessage } from '/@/hooks/message';
import { AddReceiver } from '/@/api/platform/digital/message';
import { ref, reactive } from 'vue';
import orgSelector from '/@/components/OrgSelector/index.vue';

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
// 通知父组件刷新
const emit = defineEmits(['refresh']);

const dataForm = reactive({
});
const dataRules = ref({
  userList: [
    { required: true, message: t('bar.cannotEmpty'), trigger: 'blur' },
  ]
});

// 打开弹窗
const openDialog = async (Module: string, Type: string) => {
  visible.value = true;
  dataForm.module = Module;
  dataForm.type = Type;
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    await AddReceiver(dataForm).then((res) => {
      if (!res.ok) {
        useMessage().error(res.data);
      }
    });
    visible.value = false; // 关闭弹窗
    emit('refresh');
  } catch (error: any) {
    useMessage().error(error.msg);
  } finally {
    loading.value = false;
  }
};

// 暴露变量
defineExpose({
  openDialog,
});
</script>
